<template>
  <div class="app-container">
<!--上面的输入搜索框-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="待检订单编号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入待检订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物类型名" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="货物类型名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- 创建时间范围选择 -->
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--新增,处理任务,删除按钮-->
    <el-row :gutter="10" class="mb8">



      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['quality:outQuality:export']"
        >导出</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

<!--展示数据的表格，列表-->
    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--   序号   -->
      <el-table-column label="序号" align="center" prop="id" width="100">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="待检订单编号" align="center" prop="orderNumber" />
      <el-table-column label="订单类型" align="center" prop="orderType" />
      <el-table-column label="待检货物类型名" align="center" prop="goodsName" />
      <el-table-column label="卖家名" align="center" prop="sellerName" />

      <el-table-column label="质检人" align="center" prop="qualityName" />


      <el-table-column label="状态" align="center" prop="inspectionStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lg_quality_status" :value="scope.row.inspectionStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:post:edit']"-->
<!--          >处理任务-->
<!--          </el-button>-->

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAssign(scope.row)"
            v-hasPermi="['quality:goQuality:zhipai']"
          >指派质检员</el-button>
        </template>
      </el-table-column>
    </el-table>


<!--分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或处理任务待检订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="是否异常" prop="isAbnormal">
          <el-radio-group v-model="form.isAbnormal">
            <el-radio
              v-for="dict in dict.type.lg_qualityorder_is_abnormal"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="具体异常信息" prop="abnormalMessage">
          <el-input v-model="form.abnormalMessage" placeholder="请输入具体异常信息" />
        </el-form-item>



        <el-form-item label="待检订单状态" prop="inspectionStatus">
          <el-radio-group v-model="form.inspectionStatus">
            <el-radio
              v-for="dict in dict.type.lg_quality_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 指派质检员弹框 -->
    <el-dialog title="指派质检员" :visible.sync="assignDialogVisible" width="80%" :close-on-click-modal="false" :destroy-on-close="true">
      <quality-man :orderNumber="selectedOrderNumber" @close="closeAssignDialog" />
    </el-dialog>
  </div>
</template>

<script>
import { listGoQuality, getGoQuality, updateGoQuality,insertPendingOrdersToQualityStatus } from "@/api/quality/goQuality";
import QualityMan from '@/views/quality/goQuality/qualityMan/index.vue';
export default {
  components: {
    QualityMan
  },
  name: "Post",
  dicts: ['lg_quality_status','lg_qualityorder_is_abnormal'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 待检订单表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      assignDialogVisible: false,  // 控制弹框显示
      selectedOrderNumber: null, // 存储订单编号
      taskId: null, // 存储任务 ID（可选，看业务需求）
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: undefined,
        sellerName: undefined,
        status: undefined,
        inspectionStatus: undefined,
        isAbnormal: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },

  mounted() {
    insertPendingOrdersToQualityStatus();
  },
  methods: {
    /** 指派质检员 */
    handleAssign(row) {
      this.taskId = row.id;  // 存储任务 ID，后续可用于 API 请求
      this.selectedOrderNumber = row.orderNumber; // 存储 orderNumber
      this.assignDialogVisible = true;  // 打开弹框
    },

    /** 关闭指派质检员弹窗 */
    closeAssignDialog() {
      this.assignDialogVisible = false;
      this.getList(); // 刷新列表数据
    },
    /** 查询待检订单列表 */
    getList() {
      this.loading = true;
      listGoQuality(this.queryParams).then(response => {
        this.postList = response.rows;
        console.log(response);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        orderNumber: undefined,
        goodsName: undefined,
        sellerName: undefined,
        status: undefined,
        inspectionStatus: undefined,
        isAbnormal: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加待检订单";
    },
    // /** 处理任务按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const orderNumber = row.orderNumber || this.orderNumbers
    //   getGoQuality(orderNumber).then(response => {
    //     this.form = response.data;
    //     this.form.status = this.form.status + "";
    //     this.form.inspectionStatus = this.form.inspectionStatus + "";
    //     this.form.isAbnormal = this.form.isAbnormal + "";
    //     this.open = true;
    //     this.title = "处理任务待检订单";
    //   });
    // },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderNumber != undefined) {
            updateGoQuality(this.form).then(response => {
              this.$modal.msgSuccess("处理任务成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQualityMan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('quality:goQuality:export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }

  }
};
</script>
